package parser_antlr4;

import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;

import java.util.ArrayList;
import java.util.List;

public class ErrorListener extends BaseErrorListener {

  private final List<String> errorMsgLines = new ArrayList<>();

  public boolean hasError() {
    return !errorMsgLines.isEmpty();
  }

  public String getErrorMsg() {
    return String.join(" ", errorMsgLines);
  }

  @Override
  public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
    String msg, RecognitionException e) {
    errorMsgLines.add("line " + line + ":" + charPositionInLine + " " + msg);
  }
}
